package fr.ants.controller.draganddrop;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.TransferHandler;

import fr.ants.controller.command.NewNestCommand;
import fr.ants.controller.command.NewObstacleCommand;
import fr.ants.controller.command.NewResourceCommand;
import fr.ants.view.Items;
import fr.ants.view.panel.Canvas;

/**
 * This class is used to handle the transfer of JButton.
 * 
 * @author Guillaume Ruscassie
 * 
 */
public class ButtonTransferHandler extends TransferHandler {

	/**
     * 
     */
	private static final long serialVersionUID = 7138968778184703617L;

	/**
	 * command to add a new nest.
	 */
	private final NewNestCommand newNestCommand;
	/**
	 * command to add a new resource.
	 */
	private final NewResourceCommand newResourceCommand;
	/**
	 * command to add a new obstacle.
	 */
	private final NewObstacleCommand newObstacleCommand;

	/**
	 * Builder.
	 * 
	 * @param NewNestCommand
	 * @param NewResourceCommand
	 * @param NewObstacleCommand
	 */
	public ButtonTransferHandler(final NewNestCommand newNestCommand,
			final NewResourceCommand newResourceCommand,
			final NewObstacleCommand newObstacleCommand) {
		this.newNestCommand = newNestCommand;
		this.newResourceCommand = newResourceCommand;
		this.newObstacleCommand = newObstacleCommand;
	}

	/*
	 * Import data. <p> When the method is called, it's the target of transfer
	 */
	@Override
	public boolean importData(final JComponent component,
			final Transferable transferable) {
		// To disable the function when the simulation is running
		if (hasUnitePanelFlavor(transferable.getTransferDataFlavors())) {
			try {
				final JButton button = (JButton) transferable
						.getTransferData(ButtonFlavorFactory
								.getUnitePanelFlavor());
				//button.setVisible(true);
			//	button.setBounds(null);
				final Items items = Items.getFromActionCommand(button
						.getActionCommand());
				final Canvas target = (Canvas) component;
				target.setSelectedXY((int) target.getMousePosition().getX(),
						(int) target.getMousePosition().getY());

				switch (items) {
				case NEW_NEST:
					newNestCommand.execute();
					return true;
				case OBSTACLE:
					newObstacleCommand.execute();
					return true;
				case RESOURCE:
					newResourceCommand.execute();
					return true;

				default:
					return false;
				}
			} catch (UnsupportedFlavorException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return false;
	}

	/*
	 * Creating a transferable encapsulate a record.
	 * 
	 * <p> if's the origin of transfer
	 */
	@Override
	protected Transferable createTransferable(final JComponent component) {
		return new ButtonTransferable((JButton) component);
	}

	/**
	 * 
	 * check if there is a recordFlacvo in the list.
	 * 
	 * @param flavors
	 *            DataFlavor[]
	 * @return {@link Boolean} if success else false
	 */
	protected boolean hasUnitePanelFlavor(final DataFlavor[] flavors) {
		// must be impossible
		if (ButtonFlavorFactory.getUnitePanelFlavor() == null) {
			return false;
		}

		for (int i = 0; i < flavors.length; i++) {
			if (ButtonFlavorFactory.getUnitePanelFlavor().equals(flavors[i])) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean canImport(JComponent component, DataFlavor[] flavors) {
		return hasUnitePanelFlavor(flavors);
	}

	@Override
	public int getSourceActions(JComponent component) {
		return MOVE;
	}

}
